/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

// DlgBldShutdown.cpp : implementation file
//

#include "stdafx.h"
#include "CHScanner.h"
#include "DlgBldShutdown.h"

//---------------------------------------------------------------------------

IMPLEMENT_DYNAMIC(DlgBldShutdown, CRgnDialog)

DlgBldShutdown::DlgBldShutdown(CWnd* pParent /*=NULL*/) : CRgnDialog(DlgBldShutdown::IDD, pParent)
{
	skinIn = NULL;
	retVal = false;
	dlgResult.Empty();
}

DlgBldShutdown::~DlgBldShutdown()
{
}

//---------------------------------------------------------------------------

void DlgBldShutdown::DoDataExchange(CDataExchange* pDX)
{
	CRgnDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_CHK_SHUTDOWN, chkShutdown);
	DDX_Control(pDX, IDC_CHK_FORCED, chkForced);
	DDX_Control(pDX, IDC_STA_TIMEOUT, staTimeOut);
	DDX_Control(pDX, IDC_EDT_TIMEOUT, edtTimeOut);
	DDX_Control(pDX, IDC_EDT_MESSAGE, edtMessage);
	DDX_Control(pDX, IDC_BTN_OK, btnOk);
	DDX_Control(pDX, IDC_BTN_CANCEL, btnCancel);
}

//---------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(DlgBldShutdown, CRgnDialog)
	ON_BN_CLICKED(IDC_BTN_OK, &DlgBldShutdown::OnBnClickedBtnOk)
	ON_BN_CLICKED(IDC_BTN_CANCEL, &DlgBldShutdown::OnBnClickedBtnCancel)
END_MESSAGE_MAP()

//---------------------------------------------------------------------------

BOOL DlgBldShutdown::OnInitDialog()
{
	CRgnDialog::OnInitDialog();

	SetProperties(skinIn, false, _T("Shutdown/Restart"));

	if(skin!=NULL){
		chkShutdown.textColor = skin->textColor;
		chkForced.textColor   = skin->textColor;
		staTimeOut.textColor  = skin->textColor;
	}

	WorkCfgToData();

	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
}

//---------------------------------------------------------------------------

void DlgBldShutdown::OnOK()
{
	// CRgnDialog::OnOK();
}

void DlgBldShutdown::OnCancel()
{
	// CRgnDialog::OnCancel();
}

//---------------------------------------------------------------------------

void DlgBldShutdown::OnBnClickedBtnOk()
{
	UpdateData(TRUE);
	WorkCfgFromData();

	retVal = true;
	EndDialog(IDOK);
}

void DlgBldShutdown::OnBnClickedBtnCancel()
{
	retVal = false;
	EndDialog(IDABORT);
}

//---------------------------------------------------------------------------

void DlgBldShutdown::WorkCfgToData(void)
{
	CString str, sss;
	int pos;

	bool    xShutdown, xForced;
	int     xTimeOut;
	CString xMessage;

	xShutdown = false;
	xForced   = true;
	xTimeOut  = 30;
	xMessage  = _T("");

	str = dlgResult.Trim();
	if(str.GetLength()!=0){
		if(str.GetLength()>1){
			// shutdown / reboot
			sss = str.Left(1);
			if(sss.CompareNoCase(_T("T"))==0) xShutdown = true;
			else xShutdown = false;

			// forced
			sss = str.Mid(1,1);
			if(sss.CompareNoCase(_T("T"))==0) xForced = true;
			else xForced = false;

			str = str.Mid(2);
			if(str.GetLength()>0){
				pos = str.Find(_T(" "));
				switch(pos){
					case -1:
						// only time-out specified
						sss = str;
						str.Empty();
						break;

					case 0 :
						// no time-out specified
						sss.Empty();
						str = str.Mid(1);
						break;

					default:
						sss = str.Left(pos);
						str = str.Mid(pos + 1);
						// time-out and message specified
						break;
				}

				if(!sss.IsEmpty())
					xTimeOut = _tstoi(sss);

				xMessage = str;
			}
		}
	}

	if(xMessage.GetLength()>3072)
		xMessage = xMessage.Left(3072);

	if(xTimeOut<0)
		xTimeOut = 0;
	if(xTimeOut>MAX_SHUTDOWN_TIMEOUT)
		xTimeOut = MAX_SHUTDOWN_TIMEOUT;

	chkShutdown.SetCheck(xShutdown ? BST_CHECKED : BST_UNCHECKED);

	chkForced.SetCheck(xForced     ? BST_CHECKED : BST_UNCHECKED);

	str.Format(_T("%d"), xTimeOut);
	edtTimeOut.SetWindowText(str);

	edtMessage.SetWindowText(xMessage);
}

void DlgBldShutdown::WorkCfgFromData(void)
{
	CString str, sss;
	int val;

	dlgResult.Empty();

	edtTimeOut.GetWindowText(sss);
	val = _tstoi(sss);
	if(val<0)
		val = 0;
	if(val>MAX_SHUTDOWN_TIMEOUT)
		val = MAX_SHUTDOWN_TIMEOUT;

	str.Format(_T("%s%s%d"),
		chkShutdown.GetCheck()==BST_CHECKED ? _T("T") : _T("F"),
		chkForced.GetCheck()  ==BST_CHECKED ? _T("T") : _T("F"),
		val);

	edtMessage.GetWindowText(sss);
	sss.Trim();

	if(!sss.IsEmpty()){
		str += _T(" ");
		str += sss;
	}

	dlgResult = str;
}

//---------------------------------------------------------------------------

void DlgBldShutdown::ResizeChilds(void)
{
	int bw, bh;		///< size of button controls
	int ew, eh;		///< size of edit and combobox controls
	int mh, mw;     ///< size of 'message' edit box
	int ml;			///< to vertical-align labels with edit controls
	int tw, th;		///< total required width and hight
	int cw, ch;		///< client region's width and height
	int mx, my;		///< left and top space for displaying the objects in the center of the window
	int dd;			///< space between objects
	RECT rect;
	int posx1, posx2, posy;

	dd = 2;

	btnOk.GetWindowRect(&rect);
	bw = rect.right - rect.left;
	bh = rect.bottom - rect.top;

	chkShutdown.GetWindowRect(&rect);
	ew = rect.right - rect.left;
	eh = rect.bottom - rect.top;

	edtMessage.GetWindowRect(&rect);
	mw = rect.right - rect.left;
	mh = rect.bottom - rect.top;

	staTimeOut.GetWindowRect(&rect);

	ml = eh - (rect.bottom - rect.top);
	ml /= 2;

	tw = dd + mw + dd;
	th = 6*dd + 3*eh + mh + bh;

	cw = clientRect.right - clientRect.left;
	ch = clientRect.bottom - clientRect.top;

	mx = cw - tw; mx /= 2;
	my = ch - th; my /= 2;

	posx1 = clientRect.left + dd + mx;
	posx2 = posx1 + dd + ew;
	posy  = clientRect.top + dd + my + ml;

	chkShutdown.MoveWindow(posx2, posy, ew, eh, TRUE);
	posy += (eh + dd);

	chkForced.MoveWindow(posx2, posy, ew, eh, TRUE);
	posy += (eh + dd);

	staTimeOut.MoveWindow(posx1, posy + ml, ew, eh, TRUE);
	edtTimeOut.MoveWindow(posx2, posy, ew, eh, TRUE);
	posy += (eh + dd);

	edtMessage.MoveWindow(posx1, posy, mw, mh, TRUE);
	posy += (mh + dd);

	posx2 = cw - bw - dd;
	posx1 = posx2 - bw - dd;

	btnOk.MoveWindow(posx1, posy, bw, bh, TRUE);
	btnCancel.MoveWindow(posx2, posy, bw, bh, TRUE);
}

bool DlgBldShutdown::GetMinimumSize(RECT* rectIn)
{
	if(rectIn==NULL) return false;

	rectIn->left   = 0;
	rectIn->right  = 224;
	rectIn->top    = 0;
	rectIn->bottom = 201;

	return true;
}

//---------------------------------------------------------------------------
